package com.sailor.dingding.autoconfigure;

import com.sailor.dingding.utils.DingdingSendMsgUtil;
import com.sailor.dingding.service.DingdingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(DingdingService.class)
@EnableConfigurationProperties(DingdingProperties.class)
public class DingdingAutoConfiguration {

    private DingdingProperties dingdingProperties;

    @Autowired
    public DingdingAutoConfiguration(DingdingProperties dingdingProperties) {
        this.dingdingProperties = dingdingProperties;
    }

    @Bean
    // 可以通过配置文件控制开启
    @ConditionalOnProperty(prefix = "dingding", name = "enable", havingValue = "true")
    DingdingService dingdingService() {
        return new DingdingService(dingdingProperties.getAppKey(), dingdingProperties.getAppSecret(), dingdingProperties.getAgentId());
    }

    @Bean
    DingdingSendMsgUtil dingdingSendMsg() {
        return new DingdingSendMsgUtil();
    }
}
